package ConveyorBelt.State;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import ConveyorBelt.Util.FontUtil;
import ConveyorBelt.Util.ImageUtil;


public class MainMenu extends  BasicGameState {
	public static final int stateID = 0;
	/**
	 * Tọa độ (x,y) để draw các button lên UI
	 */
	private int startGameButtonX, startGameButtonY;
	private int	howToPlayButtonX, howToPlayButtonY;
	private int	highScoresButtonX, highScoresButtonY;
	private int	exitGameButtonX, exitGameButtonY;
	private int createMapButtonX, createMapButtonY;
	private int	logoX, logoY;
	private int mouseX, mouseY;
	/**
	 * Scale của các button trong UI, mỗi khi trỏ vào thì button sẽ tăng scale lên một chut
	 */
	private float startGameScale = 1;
	private float howToPlayScale = 1; 
	private float highScoresScale = 1; 
	private float exitGameScale = 1;
	private float createMapScale = 1;

	/**
	 * Sử dụng để tăng Scale của button khi trỏ vào
	 */
	private float scaleStep = 0.0001f;
	@Override
	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		ImageUtil.loadAllImage();
		FontUtil.loadGameFont();
		startGameButtonX = gc.getWidth()/2 - ImageUtil.startGameButton.getWidth()/2;
		startGameButtonY = gc.getHeight()/2 - 200;
		createMapButtonX = startGameButtonX;
		createMapButtonY = startGameButtonY + 100;
		howToPlayButtonX = startGameButtonX;
		howToPlayButtonY = startGameButtonY + 200;
		highScoresButtonX = startGameButtonX;
		highScoresButtonY = startGameButtonY + 300;
		exitGameButtonX = startGameButtonX; 
		exitGameButtonY = startGameButtonY + 400;
		logoX = gc.getWidth()/2 - ImageUtil.logo.getWidth()/2;
		logoY = startGameButtonY - 150;
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		ImageUtil.background.draw(0, 0);
 		ImageUtil.startGameButton.draw(startGameButtonX, startGameButtonY, startGameScale);
 		ImageUtil.howToPlayButton.draw(howToPlayButtonX, howToPlayButtonY, howToPlayScale);
 		ImageUtil.highScoresButton.draw(highScoresButtonX, highScoresButtonY, highScoresScale);
 		ImageUtil.exitGameButton.draw(exitGameButtonX, exitGameButtonY, exitGameScale);
 		ImageUtil.createMapButton.draw(createMapButtonX, createMapButtonY, createMapScale);
 		ImageUtil.logo.draw(logoX, logoY);
 		g.setFont(FontUtil.uf);
 		g.drawString("OOP 2D Game Project:      Can You Help Me???      - Author: Phu - Manh - Quang", 225, 650);
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		Input input = gc.getInput();
		mouseX = input.getMouseX();
		mouseY = input.getMouseY();
		boolean insideStartGame = false; 
		boolean insideHowToPlay = false;
		boolean insideHighScores = false;
		boolean insideExitGame = false;
		boolean insideCreateMap = false;
		int tmpX;
		int tmpY;

		// ------------------------------------------------//
		
		if((mouseX >= startGameButtonX && mouseX <= startGameButtonX + ImageUtil.startGameButton.getWidth()) && 
				(mouseY >= startGameButtonY && mouseY <= startGameButtonY + ImageUtil.startGameButton.getHeight())) {
			insideStartGame = true;
		}
		tmpY = gc.getHeight()/2 - 200;
		if(insideStartGame) {
			tmpX = gc.getWidth()/2 - ImageUtil.startGameButton.getWidth()/2 + 10;
			if(startGameScale < 1.05f) startGameScale += scaleStep*delta;
			if(startGameButtonX < tmpX) startGameButtonX ++;
			if(startGameButtonY >= tmpY) startGameButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
				sbg.getState(Game.stateID).init(gc, sbg);
				sbg.enterState(Game.stateID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
			}
		}
		else {
			tmpX = gc.getWidth()/2 - ImageUtil.startGameButton.getWidth()/2;
			if(startGameScale > 1.0f) startGameScale -= scaleStep*delta;
			if(startGameButtonX > tmpX) startGameButtonX--;
			if(startGameButtonY <= tmpY) startGameButtonY++;
		}
		// ------------------------------------------------//

		if((mouseX >= createMapButtonX && mouseX <= createMapButtonX + ImageUtil.createMapButton.getWidth()) && 
				(mouseY >= createMapButtonY && mouseY <= createMapButtonY + ImageUtil.createMapButton.getHeight())) {
			insideCreateMap = true;
		}
		tmpY = gc.getHeight()/2 - 100;
		if(insideCreateMap) {
			tmpX = gc.getWidth()/2 - ImageUtil.createMapButton.getWidth()/2 + 10;
			if(createMapScale < 1.05f) createMapScale += scaleStep*delta;
			if(createMapButtonX < tmpX) createMapButtonX ++;
			if(createMapButtonY >= tmpY) createMapButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) sbg.enterState(CreateMap.stateID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
		}
		else {
			tmpX = gc.getWidth()/2 - ImageUtil.createMapButton.getWidth()/2;
			if(createMapScale > 1.0f) createMapScale -= scaleStep*delta;
			if(createMapButtonX > tmpX) createMapButtonX--;
			if(createMapButtonY <= tmpY) createMapButtonY++;
		}
		// ------------------------------------------------//
		if((mouseX >= howToPlayButtonX && mouseX <= howToPlayButtonX + ImageUtil.howToPlayButton.getWidth()) && 
				(mouseY >= howToPlayButtonY && mouseY <= howToPlayButtonY + ImageUtil.howToPlayButton.getHeight())) {
			insideHowToPlay = true;
		}
		tmpY = gc.getHeight()/2;
		if(insideHowToPlay) {
			tmpX = gc.getWidth()/2 - ImageUtil.howToPlayButton.getWidth()/2;

			if(howToPlayScale < 1.05f) howToPlayScale += scaleStep*delta;
			if(howToPlayButtonX < tmpX) howToPlayButtonX ++;
			if(howToPlayButtonY >= tmpY) howToPlayButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) sbg.enterState(HowToPlayTable.stateID, new FadeOutTransition(Color.black),  new FadeInTransition(Color.black));
		}
		else {
			tmpX = gc.getWidth()/2 - ImageUtil.howToPlayButton.getWidth()/2;
			if(howToPlayScale > 1.0f) howToPlayScale -= scaleStep*delta;
			if(howToPlayButtonX > tmpX) howToPlayButtonX--;
			if(howToPlayButtonY <= tmpY) howToPlayButtonY++;
		}
		// ------------------------------------------------//
		if((mouseX >= highScoresButtonX && mouseX <= highScoresButtonX + ImageUtil.highScoresButton.getWidth()) && 
				(mouseY >= highScoresButtonY && mouseY <= highScoresButtonY + ImageUtil.highScoresButton.getHeight())) {
			insideHighScores = true;
		}
		tmpY = gc.getHeight()/2 + 100;
		if(insideHighScores) {
			tmpX = gc.getWidth()/2 - ImageUtil.highScoresButton.getWidth()/2 + 10;
			if(highScoresScale < 1.05f) highScoresScale += scaleStep*delta;
			if(highScoresButtonX < tmpX) highScoresButtonX ++;
			if(highScoresButtonY >= tmpY) highScoresButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
				sbg.getState(ScoreTable.stateID).init(gc, sbg);
				sbg.enterState(ScoreTable.stateID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
			}
		}
		else {
			tmpX = gc.getWidth()/2 - ImageUtil.highScoresButton.getWidth()/2;
			if(highScoresScale > 1.0f) highScoresScale -= scaleStep*delta;
			if(highScoresButtonX > tmpX) highScoresButtonX--;
			if(highScoresButtonY <= tmpY) highScoresButtonY++;
		}
		// ------------------------------------------------//
		if((mouseX >= exitGameButtonX && mouseX <= exitGameButtonX + ImageUtil.exitGameButton.getWidth()) && 
				(mouseY >= exitGameButtonY && mouseY <= exitGameButtonY + ImageUtil.exitGameButton.getHeight())) {
			insideExitGame = true;
		}
		tmpY = gc.getHeight()/2 + 200;
		if(insideExitGame) {
			tmpX = gc.getWidth()/2 - ImageUtil.exitGameButton.getWidth()/2 + 10;
			if(exitGameScale < 1.05f) exitGameScale += scaleStep*delta;
			if(exitGameButtonX < tmpX) exitGameButtonX ++;
			if(exitGameButtonY >= tmpY) exitGameButtonY --;
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) gc.exit();
		}
		else {
			tmpX = gc.getWidth()/2 - ImageUtil.exitGameButton.getWidth()/2;
			if(exitGameScale > 1.0f) exitGameScale -= scaleStep*delta;
			if(exitGameButtonX > tmpX) exitGameButtonX--;
			if(exitGameButtonY <= tmpY) exitGameButtonY++;
		}
	}

	@Override
	public int getID() {
		return stateID;
	}
	
}

